package base.day26.tcp;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadDome1 implements Runnable{
    private Socket accept;
    public ThreadDome1(Socket accept) {
        this.accept=accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos=null;
        try {
            BufferedInputStream bis=new BufferedInputStream(accept.getInputStream());
            bos=new BufferedOutputStream(new FileOutputStream("C:\\Users\\SuperJohn\\Desktop\\"+ UUID.randomUUID().toString()+".txt"));
            int b;
            while ((b=bis.read())!=-1){

                bos.write(b);
                System.out.println("------");
            }

            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();


        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if((bos!=null)){
                try {
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            System.out.println("===========");
        }
    }
}
